package services;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import play.libs.Codec;

import com.aton.db.SessionFactory;

import models.DrBroadcast;
import models.DrBroadcastExample;
import models.SystemLog;
import models.SystemOperator;
import models.mapper.DrBroadcastMapper;
import models.mapper.SystemLogMapper;

/**
 * 语音播报服务类
 * @author wangshaoming
 *
 */
public class DrBroadCastService {
	/**
	 * 获取语音播报列表
	 * @param operatorid
	 * @return
	 */
	public static List<DrBroadcast> getList(String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrBroadcastMapper mapper=ss.getMapper(DrBroadcastMapper.class);
			DrBroadcastExample example=new DrBroadcastExample();
			DrBroadcastExample.Criteria c=example.createCriteria();
			if(StringUtils.isNotEmpty(operatorid)){
				c.andCreatenameEqualTo(operatorid);
			}
			example.setOrderByClause("createtime desc");
			return mapper.selectByExample(example);
		}finally{
			ss.close();
		}
	}
	/**
	 * 获取语音播报标题列表
	 * @param operatorid
	 * @return
	 */
	public static List<DrBroadcast> getTitleList(String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrBroadcastMapper mapper=ss.getMapper(DrBroadcastMapper.class);
			return mapper.getTitleList(operatorid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存语音播报
	 * @param cast
	 * @param ip 
	 * @param opr 
	 * @return
	 */
	public static boolean saveBroad(DrBroadcast cast, SystemOperator opr, String ip) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSessionWithoutAutoCommit();
		try{
			//保存语音播报
			DrBroadcastMapper mapper=ss.getMapper(DrBroadcastMapper.class);
			mapper.insert(cast);
			//插入日志
			SystemLog log=new SystemLog(Codec.UUID(),opr.getOperatorid(),ip,"保存语音播报","保存语音播报"+cast.getTitle(),new Date());
			SystemLogMapper logMapper=ss.getMapper(SystemLogMapper.class);
			logMapper.insert(log);
			ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 根据id获取文本内容
	 * @param id
	 * @return
	 */
	public static DrBroadcast getContentById(String id) {
		// TODO Auto-generated method stub
		SqlSession ss=SessionFactory.getSqlSession();
		try{
			DrBroadcastMapper mapper=ss.getMapper(DrBroadcastMapper.class);
			return mapper.getContentById(id);
		}finally{
			ss.close();
		}
	}
	/**
	 * 删除播报记录
	 * @param id
	 * @return
	 */
	public static boolean delBroad(String id) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.INSTANCE.getSqlSessionWithoutAutoCommit();
		try{
			DrBroadcastMapper mapper=ss.getMapper(DrBroadcastMapper.class);
			mapper.deleteByPrimaryKey(id);
			ss.commit();
		}catch(Exception e ){
			e.printStackTrace();
			ss.rollback();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
}
